package com.john.tests.gdx;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;

public class OrthographicCameraExample implements ApplicationListener {
	
	private final int TEXTURE_WIDTH = 2048;
	private final int TEXTURE_HEIGHT = 2048;
		
	private final int REGION_WIDTH = 1920;
	private final int REGION_HEIGHT = 1200;
	
	private final int VIEW_WIDTH = 320;
	private final int VIEW_HEIGHT = 208;
	
	private final int FRAMES_PER_SECOND = 30;
	
	private final float ROTATION = 5f;
	
	private final int STEP = 16;
	
	private Mesh mMesh;
	private Texture mTexture;
	private OrthographicCamera mCamera;
	
	private Rectangle mViewport;
	private Vector3 mCenterPoint;
	private float mElapsedTime;

	@Override
	public void create() {
		final int textureHalfWidth = TEXTURE_WIDTH / 2; 
		final int textureHalfHeight = TEXTURE_HEIGHT / 2;
		
		final int left = (-1) * textureHalfWidth;
		final int top = textureHalfHeight;
		final int right = (REGION_WIDTH - textureHalfWidth);
		final int bottom = (-1) * (REGION_HEIGHT - textureHalfHeight);
		
		final float[] vertices = new float[] {
				/*left, bottom, 0, 0, 1,
				right, bottom, 0, 1, 1, 
			  	right, top, 0, 1, 0,
			  	left, top, 0, 0, 0*/
			  	-1024f, -1024f, 0, 0, 1,
                1024f, -1024f, 0, 1, 1,
                1024f,  1024f, 0, 1, 0,
                -1024f,  1024f, 0, 0, 0
		};
		
		mMesh = new Mesh(true, 4, 6, 
						 new VertexAttribute(VertexAttributes.Usage.Position, 3,"attr_Position"),
						 new VertexAttribute(Usage.TextureCoordinates, 2, "attr_texCoords"));
		
		mMesh.setVertices(vertices);
		mMesh.setIndices(new short[] {0, 1, 2, 2, 3, 0});
		
		mTexture = new Texture(Gdx.files.internal("data/texture.png"));
		
		mCenterPoint = new Vector3(VIEW_WIDTH / 2, VIEW_HEIGHT / 2, 0);
		
		mCamera = new OrthographicCamera(VIEW_WIDTH, VIEW_HEIGHT);
		mCamera.position.set(mCenterPoint);
		
		mViewport = new Rectangle(0, 0, VIEW_WIDTH, VIEW_HEIGHT);
		
		mElapsedTime = 0;
	}
	
	@Override
	public void render() {
		mElapsedTime += Gdx.graphics.getDeltaTime();
		
		if (mElapsedTime > (1.f / FRAMES_PER_SECOND)) {
			mElapsedTime = 0;
			handleInput();
		}
		
		GL10 gl = Gdx.graphics.getGL10();
		
		gl.glClearColor(0.09804f, 0.6274f, 0.8784f, 1f);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		gl.glViewport((int) mViewport.x, (int) mViewport.y,
					  (int) mViewport.width, (int) mViewport.height);
		
		mCamera.update();
        mCamera.apply(gl);
        
        gl.glActiveTexture(GL10.GL_TEXTURE0);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        mTexture.bind();
        
        mMesh.render(GL10.GL_TRIANGLES);
	}

	@Override
	public void resize(int arg0, int arg1) {
		
	}

	@Override
	public void pause() {
		
	}

	@Override
	public void resume() {
		
	}
	
	@Override
	public void dispose() {
		
	}
	
	private void handleInput() {
		if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
		   mCamera.translate(-STEP, 0, 0);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
		  mCamera.translate(STEP, 0, 0);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
			mCamera.translate(0, -STEP, 0);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.UP)) {
			mCamera.translate(0, STEP, 0);
		}
		
		if(Gdx.input.isKeyPressed(Input.Keys.A)) {
			mCamera.zoom += 0.02;
	    }
	    if(Gdx.input.isKeyPressed(Input.Keys.Q)) {
	    	mCamera.zoom -= 0.02;
	    }

	    if(Gdx.input.isKeyPressed(Input.Keys.W)) {
	    	mCamera.rotate(-ROTATION, 0, 0, 1);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.E)) {
			mCamera.rotate(ROTATION, 0, 0, 1);
		}

	    
	}

}
